/**
 * description
 * <p>fill description</p>
 * @author zido
 * @since 2017/6/3 0003
 */
export const OPEN_MODAL_ADD_BUSINESS = 'OPEN_MODAL_ADD_BUSINESS'
export const POST_MODAL_LOADING = 'POST_MODAL_LOADING'
export const LOAD_DATA = 'LOAD_DATA'
export const SAVE_TABLE_OPTIONS = 'SAVE_TABLE_OPTIONS'
export const LOAD_DETAIL_DATA = 'LOAD_DETAIL_DATA'
export const SAVE_USER_INFO = 'SAVE_USER_INFO'
export const PASS = 'PASS'
export const DELETE_BY_BUSINESS_USER_ID = 'DELETE_BY_BUSINESS_USER_ID'
import {
  post,
  isEmpty
} from '../../../../Util'
import {
  PAGE_SIZES as pageSizes
} from '../../../../Constants'

const openAddModal = (isOpen = true, id) => {
  if (isOpen && !isEmpty(id)) {
    return (dispatch) => {
      dispatch({
        type: OPEN_MODAL_ADD_BUSINESS,
        openAdd: false,
        editData: {},
        seeLoading: true,
        loadingId: id,
      })
      return post('api/center/business/get/info?id=' + id).then(result => {
        dispatch({
          type: OPEN_MODAL_ADD_BUSINESS,
          openAdd: isOpen,
          editData: result.data,
          seeLoading: false,
          editDate: id,
        })
      })
    }
  } else {
    return ({
      type: OPEN_MODAL_ADD_BUSINESS,
      openAdd: isOpen,
      loadingId: id,
    })
  }
}

/*
 * ({
 type:OPEN_MODAL_ADD_BUSINESS,
 isOpen:isOpen,
 data:id,
 })
 *
 * */

const postAddData = (data) => {
  return (dispatch) => {
    dispatch({
      type: POST_MODAL_LOADING,
      modalLoading: true,
    })
    return post('/api/center/business/add', data).then((result) => {
      dispatch({
        type: POST_MODAL_LOADING,
        modalLoading: false,
        openAdd: false,
        message: result.message,
      })
    })
  }
}
const deleteByBusinessUserId = (id) => {
  return (dispatch) => {
    dispatch({
      type: DELETE_BY_BUSINESS_USER_ID,
      deleteLoading: true,
      deleteLoadingId: id,
    })
    return post('/api/center/business/delete?id=' + id).then(result => {
      dispatch({
        type: DELETE_BY_BUSINESS_USER_ID,
        deleteLoading: false,
        deleteLoadingId: id,
        message: result.message,
      })
    })
  }
}
const saveTableOptions = (options) => (dispatch) => {
  dispatch({
    type: SAVE_TABLE_OPTIONS,
    data: options,
    loading: true,
  })
  const {
    pagination,
    filters,
    sorter
  } = options
  return post('/api/center/business/get/list?currentPage' + options.pagination.current + '&level=' + pageSizes.indexOf(pagination.pageSize), {
    sortName: sorter.field,
    desc: sorter.order !== 'ascend',
    ...filters,
  }).then(result => {
    dispatch({
      type: LOAD_DATA,
      data: result.data,
      loading: false,
    })
  })
}

/*
 *
 * */

const loadData = (currentPage = 1, level = 0, condition) => {
  return (dispatch) => {
    dispatch({
      type: LOAD_DATA,
      loading: true
    })
    return post('/api/center/business/get/list?currentPage=' + currentPage + '&level=' + level, condition).then(result => {
      dispatch({
        type: LOAD_DATA,
        data: result.data,
        loading: false,
      })
    })
  }
}

const pass = (id, pass) => (dispatch) => {
  dispatch({
    type: PASS,
    passLoading: true,
    passLoadingId: id,
    isPass: pass
  })
  return post('/api/center/business/pass?id=' + id + '&pass=' + pass).then(result => {
    dispatch({
      type: PASS,
      message: result.message,
      loading: false,
      isPass: pass,
    })

  })
}

const saveUserInfo = (form, userId, businessUserId, shopKeys, banKeys) => ({
  type: SAVE_USER_INFO,
  userInfo: {
    form,
    userId,
    businessUserId,
    shopKeys,
    banKeys
  }
})

const ACTION_HANDLERS = {
  [OPEN_MODAL_ADD_BUSINESS]: (state, action) => ({
    ...state,
    openAdd: action.openAdd,
    editData: action.editData,
    seeLoading: action.seeLoading,
    loadingId: action.loadingId,
  }),
  [LOAD_DATA]: (state, action) => {
    return {...state,
      data: action.data || {},
      loading: action.loading
    }
  },
  [SAVE_TABLE_OPTIONS]: (state, action) => ({
    ...state,
    tableOptions: action.data,
    loading: action.loading,
  }),
  [POST_MODAL_LOADING]: (state, action) => ({...state,
    ...action
  }),
  [SAVE_USER_INFO]: (state, action) => ({...state,
    ...action.userInfo
  }),
  [DELETE_BY_BUSINESS_USER_ID]: (state, action) => ({...state,
    ...action
  })
}

const initialState = {
  openAdd: false,
  modalLoading: false,
  editData: {},
  data: [],
  loading: false,
  tableOptions: {
    pagination: {},
    filters: {},
    sorter: {},
  },
  loadingId: '0',
  seeLoading: false,
  passLoadingId: '0',
  passLoading: false,
  isPass: true,
  deleteLoading: false,
  deleteLoadingId: null,
}
export const HANDLERS = ((state = initialState, action) => {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
})
export {
  HANDLERS as
  default,
  openAddModal,
  postAddData,
  loadData,
  saveTableOptions,
  saveUserInfo,
  pass,
  deleteByBusinessUserId,
}